package com.yami.shop.live.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.live.common.model.LiveRoomProd;
import com.yami.shop.live.common.service.LiveRoomProdService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author lhd
 * @date 2020-08-05 08:53:17
 */
@RestController
@RequestMapping("/live/liveRoomProd")
@Tag(name = "平台端直播间商品接口")
public class LiveRoomProdController {

    @Autowired
    private LiveRoomProdService liveRoomProdService;

    @GetMapping("/page")
    @Operation(summary =  "分页查找直播间商品信息")
    public ServerResponseEntity<IPage<LiveRoomProd>> getLiveRoomProdPage(PageParam<LiveRoomProd> page,@ParameterObject LiveRoomProd liveRoomProd) {
        return ServerResponseEntity.success(liveRoomProdService.page(page, new LambdaQueryWrapper<LiveRoomProd>()));
    }

    @GetMapping("/info/{roomProdId}")
    @Operation(summary =  "通过id查询直播间商品库")
    @Parameter(name = "roomProdId", description = "直播间商品id", required = true)
    public ServerResponseEntity<LiveRoomProd> getById(@PathVariable("roomProdId") Long roomProdId) {
        return ServerResponseEntity.success(liveRoomProdService.getById(roomProdId));
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('live:liveRoomProd:save')")
    @Operation(summary =  "新增直播间商品")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid LiveRoomProd liveRoomProd) {
        return ServerResponseEntity.success(liveRoomProdService.save(liveRoomProd));
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('live:liveRoomProd:update')")
    @Operation(summary =  "修改直播间商品")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid LiveRoomProd liveRoomProd) {
        return ServerResponseEntity.success(liveRoomProdService.updateById(liveRoomProd));
    }

    @DeleteMapping("/{roomProdId}")
    @PreAuthorize("@pms.hasPermission('live:liveRoomProd:delete')")
    @Operation(summary =  "通过id删除直播间商品库")
    @Parameter(name = "roomProdId", description = "直播间商品id", required = true)
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long roomProdId) {
        return ServerResponseEntity.success(liveRoomProdService.removeById(roomProdId));
    }
}
